<?php
if (!defined('WEB_ROOT')) {
	exit;
}
if (isset($_GET['nn_id']) && $_GET['nn_id'] > 0) {
	$nn_id = (int)$_GET['nn_id'];
	$queryString = "&nn_id=$nn_id";
} else {
	$nn_id = 0;
	$queryString = '';
}

$filter_cond = "";
if (isset($_POST['capth']) && $_POST['capth'] > 0) { 
	$lv_id = (int)$_POST['capth']; 
	$filter_cond .= " AND cap_id = $lv_id ";
} else {
	$lv_id = 0;
}
if (isset($_POST['donvi']) && $_POST['donvi'] > 0) { 
	$dv_id = (int)$_POST['donvi']; 
	$filter_cond .= " AND dv_id = $dv_id ";
} else {
	$dv_id = 0;
}
if (isset($_POST['nam']) && $_POST['nam'] > 0) { 
	$ye_id = (int)$_POST['nam']; 
	$filter_cond .= " AND namthuchien = $ye_id ";
} else {
	$ye_id = 0;
}
if (isset($_POST['tt']) && $_POST['tt'] > 0) { 
	$tt_id = (int)$_POST['tt']; 
	$filter_cond .= " AND tt_id = $tt_id ";
} else {
	$tt_id = 0;
}

$deptList = buildDeptOptions($dv_id);
$levelList = buildLevelOptions($lv_id);
$yearList = buildYearOptions($ye_id);
$stateList = buildStateOptions($tt_id);
//$dv_id = (int)$_POST['cboDept']; echo $dv_id;
	
// for paging
// how many rows to show per page
$rowsPerPage = 5;

if ($filter_cond != '')
{
	$filter_cond = " WHERE true $filter_cond ";
}

$sql = "SELECT distinct dt.id, madt, tendt, date_format(ngaybatdau,'%d-%m-%Y') as ngaybatdau, date_format(ngayketthuc,'%d-%m-%Y') as  ngayketthuc,  cap_id, tendv, tentt, nn_id
        FROM detai dt left join tacgia_detai tgd ON dt.id = tgd.dt_id left join donvi dv on dt.dv_id = dv.id join trangthai tt on dt.tt_id = tt.id
		$filter_cond and dt.hieu_luc = 1
		ORDER BY dt.id desc";
		
//echo $sql;
$result     = dbQuery(getPagingQuery($sql, $rowsPerPage));
$pagingLink = getPagingLink($sql, $rowsPerPage);
		

?> 

<!-- Begin one column window -->
<div class="onecolumn">
				<div class="header">
					<span>Đề tài nghiên cứu khoa học</span>
					<div class="switch" style="width:160px">
						<table width="140px" cellpadding="0" cellspacing="0">
						<tbody>
							<tr>
								<td>
									<input name="btnAddTopic" class="middle_switch" type="button" id="btnAddTopic" value=" Thêm đề tài "  style="width:140px" onClick="addTopic()">
								</td>
							</tr>
						</tbody>
						</table>
					</div>
				</div>
				<br class="clear"/>
				<div class="content">

<form action="index.php?view=list" method="post"  name="frmListTopic" id="frmListTopic">
	<table width="100%" border="0" cellspacing="0" cellpadding="2" class="text">
	  <tr>
	   <td  align="right"> 
		<select name="capth" class="box" id="cbocapth" onChange="frmListTopic.submit();">
		 <option>--Cấp thực hiện--</option>
		<?php echo $levelList; ?>
	   </select> </td>
	  <td align="right"> 
		<select name="donvi" size="1" class="box" id="cbodonvi" onChange="frmListTopic.submit();">
		 <option>--Đơn vị--</option>
		<?php echo $deptList; ?>
	   </select>
	 </td>
	  <td  align="right"> 
		<select name="nam" class="box" id="cbonam" onChange="frmListTopic.submit();">
		 <option>--Năm thực hiện--</option>
		<?php echo $yearList; ?>
	   </select>
	 </td>
	 <td  align="right"> 
		<select name="tt" class="box" id="cbott" onChange="frmListTopic.submit();">
		 <option>--Xem tất cả--</option>
		<?php echo $stateList; ?>
	   </select>
	 </td>
	 </tr>
	</table>
</form>
<br />
<form action="processTopic.php?action=addTopic" method="post"  name="frmAddTopic" id="frmAddTopic">
 <table width="100%" border="0" align="center" cellpadding="2" cellspacing="1" class="data">
	  <tr id="listTableHeader"> 
		   <th>Thông tin đề tài</th>
			<!--   <td width="75">Hình ảnh</td>-->
		   <th width="150px">Thời gian</th>
		   <th width="40px">Cấp</th>
		   <th width="70">Trạng thái</th>
	  </tr>
  <?php
if (dbNumRows($result) > 0) {
	$i = 0;
	
	while($row = dbFetchAssoc($result)) {
		extract($row);
		if ($i%2) {
			$class = 'row1';
		} else {
			$class = 'row2';
		}	
		$i += 1;
?>
  <tr class="<?php echo $class; ?>"> 
 	   <td>
			<div class="info_detail_noimage">
				<ul>
					<li>Mã đề tài&nbsp;&nbsp;&nbsp;: &nbsp;<strong><?php echo $madt; ?></strong></li>
					<li>Tên đề tài&nbsp;: &nbsp;<strong><a href="index.php?view=detail&id=<?php echo $id; ?>"><?php echo $tendt; ?></strong></a></li>
					<li>Tên đơn vị:&nbsp;<strong><?php echo $tendv; ?></strong></li>
					
				</ul>
				<div class='action'>&nbsp;&nbsp;
				<a href="javascript:modifyTopic(<?php echo $id; ?>, <?php echo (int)$nn_id; ?>);">Sửa</a>&nbsp;&nbsp;|&nbsp;&nbsp;
				<a href="javascript:deleteTopic(<?php echo $id;?>);">Xóa</a></div>
			</div>
		</td>
	   <td width="150px">
			<div>Ngày bắt đầu&nbsp;:&nbsp;<?php echo $ngaybatdau; ?></div>
			<div class="gach_chan"></div>
			<div>Ngày kết thúc:&nbsp;<?php echo $ngayketthuc; ?></div>
		</td>
	   <td width="40" align="center"><?php echo $cap_id; ?></td>
	   <td width="70" align="center"><?php echo $tentt;?></td>
</tr>
  <?php
	} // end while
?>
  <tr> 
   <td colspan="5" align="center">
   <?php 
echo $pagingLink;
   ?></td>
  </tr>
<?php	
} else {
?>
  <tr> 
   <td colspan="5" align="center">Không có đề tài nghiên cứu khoa học nào</td>
  </tr>
  <?php
}
?>

 </table>
 <p>&nbsp;</p>
</form>